package openapi

import (
	"fmt"
	"sort"
	"strings"
)

type AscOrderedMap map[string]interface{}
type DescOrderedMap map[string]interface{}

type Item struct {
	Key   string
	Value interface{}
	Desc  bool
}
type Items []Item

func (i Items) Len() int {
	return len(i)
}

func (i Items) Less(first, second int) bool {
	return (strings.Compare(i[first].Key, i[second].Key) < 0) != i[first].Desc
}

func (i Items) Swap(first, second int) {
	var temp = i[first]
	i[first] = i[second]
	i[second] = temp
}

func (it Item) String() string {
	return fmt.Sprintf("%s=%v", it.Key, it.Value)
}

func (a AscOrderedMap) Strings() []string {
	var keys = make(Items, len(a))
	var i = 0
	for k, v := range a {
		keys[i] = Item{Key: k, Value: v}
		i++
	}
	sort.Sort(keys)
	var result = make([]string, len(keys))
	for k, v := range keys {
		result[k] = v.String()
	}
	return result
}

func (a DescOrderedMap) Strings() []string {
	var keys = make(Items, len(a))
	var i = 0
	for k, v := range a {
		keys[i] = Item{Key: k, Value: v, Desc: true}
		i++
	}
	sort.Sort(keys)
	var result = make([]string, len(keys))
	for k, v := range keys {
		result[k] = v.String()
	}
	return result
}
